<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\WxMessage;
use App\Models\RcUser;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Widgets\Alert;
use Dcat\Admin\Widgets\Callout;
use Dcat\Admin\Widgets\Card;
use Faker\Factory;
use GuzzleHttp\Client;
use Illuminate\Support\Facades\Route;

class WxMessageController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new WxMessage(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('template_id');
            $grid->column('data');
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();
            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->append('<a href="'.env('APP_URL').'/admin/send_message"><i class="fa fa-eye"></i>发送模板消息</a>');
            });
            $grid->disableDeleteButton();
//            $grid->disableEditButton();
            $grid->disableQuickEditButton();
//            $grid->disableViewButton();
            $grid->disableCreateButton();
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
        
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new WxMessage(), function (Show $show) {
            $show->field('id');
            $show->field('template_id');
            $show->field('data');
            $show->field('created_at');
            $show->field('updated_at');

        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new WxMessage(), function (Form $form) {
            $form->display('id');
            $form->text('template_id');
            $form->textarea('data');
            $form->disableDeleteButton();
        });
    }

    public function SendMessage(Content $content)
    {
        $template =  \App\Models\WxMessage::query()->first();
        $token = env('WX_TOKEN');
        $client = new Client(['','timeout'  => 5.0,]);
        //拿到微信access_token
        $response1 = $client->get('https://amxshop.yili.com/api/user/getAccessToken',['headers'=>['accessToken'=>$token]]);
        $data = json_decode($response1->getBody()->getContents());
        $wx_token = $data->data;
        \App\Jobs\WxMessage::dispatch($template,$wx_token,$client);
        $content->row(Alert::make('发送成功', 'Success')->success());
        $header = '模板消息';
        $content->breadcrumb('Components');
        $content->breadcrumb($header);
        return $content->header($header);
//        return '模板消息已发送';
//        return redirect('/admin/wx_message');
    }

    public function SendMessage2(Content $content)
    {
        dd(11);
        $template =  \App\Models\WxMessage::query()->first();
        $token = env('WX_TOKEN');
        $client = new Client(['','timeout'  => 5.0,]);
        //拿到微信access_token
        $response1 = $client->get('https://amxshop.yili.com/api/user/getAccessToken',['headers'=>['accessToken'=>$token]]);
        $data = json_decode($response1->getBody()->getContents());
        $wx_token = $data->data;
        \App\Jobs\WxMessage::dispatch($template,$wx_token,$client);
        $content->row(Alert::make('发送成功', 'Success')->success());
        $header = '模板消息';
        $content->breadcrumb('Components');
        $content->breadcrumb($header);
        return $content->header($header);
//        return '模板消息已发送';
//        return redirect('/admin/wx_message');
    }
}
